Module modDB

    Friend Const REGAPP As String = "WRDB"

    ''' <summary>
    ''' Name of directory for temporary files (does NOT end with backslash)
    ''' </summary>
    Friend TempPath As String = String.Format("{0}\WRDB", My.Computer.FileSystem.SpecialDirectories.Temp)

    ''' <summary>
    ''' Test object; if dbnull, nothing or empty string, return default value
    ''' </summary>
    Friend Function TestNull(ByVal Value As Object, ByVal DefaultValue As Object) As Object
        If IsDBNull(Value) OrElse IsNothing(Value) OrElse (TypeOf Value Is String AndAlso Value = "") Then Return DefaultValue Else Return Value
    End Function

    Friend Declare Auto Function GetShortPathName Lib "kernel32.dll" (ByVal path As String, ByVal shortname As System.Text.StringBuilder, ByVal maxlen As Integer) As Integer

    ''' <summary>
    ''' Create comma-separated value list from array of strings
    ''' </summary>
    ''' <param name="ar">Array of strings</param>
    ''' <returns>CSV list</returns>
    Friend Function ArrayToCSV(ByVal ar() As String) As String
        Dim csv As String = ""
        For Each s As String In ar
            csv &= IIf(csv = "", "", ",") & s.Trim
        Next
        Return csv
    End Function

    ''' <summary>
    ''' Create comma-separated value list from list
    ''' </summary>
    ''' <param name="List">List of strings</param>
    ''' <returns>CSV list</returns>
    Friend Function ArrayToCSV(ByVal List As Generic.List(Of String)) As String
        Return ArrayToCSV(List.ToArray)
    End Function

    ''' <summary>
    ''' Reformat SQL statement so it looks better in a dialog box
    ''' </summary>
    ''' <param name="Sql">SQL string to be formatted</param>
    Friend Function FormatSQL(ByVal Sql As String) As String
        Return Sql.Replace(" FROM ", "\n  FROM ").Replace(" WHERE ", "\n  WHERE ").Replace(" AND ", "\n    AND ").Replace(" ORDER ", "\n  ORDER ").Replace(" GROUP ", "\n  GROUP ").Replace(" LEFT OUTER JOIN ", "\n  LEFT OUTER JOIN ").Replace(" UNION ", "\nUNION\n")
    End Function

    ''' <summary>
    ''' Given date-time value return short date-time format string that obeys culture preferences (for US, gives MM/dd/yyyy HH:mm)
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Function MyDateTimeFormat() As String
        With My.Application.Culture.DateTimeFormat
            Return MyDateFormat() & " HH:mm"
        End With
    End Function

    ''' <summary>
    ''' Given date-time value return short date format string that obeys culture preferences (for US, gives MM/dd/yyyy HH:mm)
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Function MyDateFormat() As String
        With My.Application.Culture.DateTimeFormat
            Return IIf(My.Application.Culture.Name = "en-US", "MM/dd/yyyy", .ShortDatePattern)
        End With
    End Function

End Module
